package com;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;

/**
 *
 * @author alrz
 */
public class Zone extends JPanel implements MouseListener, MouseMotionListener
{
    public static int SIZE = 3;
    public static boolean crearJuego;
    private ArrayList<Figure> ArrayFigure; 
    private Grid gridIzquierdo;
    private Grid gridDerecho;
    private int posX;
    private int posY;
    
    
    public Zone()
    {
        super();
        super.setLayout(null);
        this.crearJuego = true;
        this.ArrayFigure = new ArrayList<Figure>();
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }
    
    @Override
    public void paint(Graphics g)
    {
      
        super.paint(g);
        
        if(crearJuego)
        {
            this.crearJuego = false;
            this.createZone();
            gridIzquierdo = new Grid(SIZE);
            gridDerecho = new Grid(SIZE);
            this.add(gridIzquierdo);
            this.add(gridDerecho);
            gridIzquierdo.setBounds(0, 0, (100*(SIZE+1)),(100*(SIZE+1)));
            gridDerecho.setBounds((SIZE*100)+100, 0, (100*(SIZE+1)),(100*(SIZE+1)));
        }
    }
    
    @Override
    public void update(Graphics g)
    {
        super.paint(g);
    }
    
    public void createZone()
    {
        Figure matriz[][] = new Figure[SIZE][SIZE];
        Random s = new Random();
        
        for(int i = 0 ; i< SIZE; i++)
        {
            for(int j = 0 ; j < SIZE  ; j++)
            {
                Figure figuraTemp = new Figure(s.nextInt(10),s.nextInt(10),s.nextInt(10),s.nextInt(10));
                matriz[i][j] = figuraTemp;
                if(j>0)
                {
                    matriz[i][j].setLeft(matriz[i][j-1].getRight());
                }
                if(i>0)
                {
                    matriz[i][j].setUp(matriz[i-1][j].getDown());
                }
            }
        }
        int nAzar = s.nextInt(SIZE*SIZE)+1;
        int i = 0;
        int x1,y1,x2,y2;
        
        while(i<nAzar)
        {
            x1 = s.nextInt(SIZE);
            y1 = s.nextInt(SIZE);
            x2 = s.nextInt(SIZE);
            y2 = s.nextInt(SIZE);
            Figure temp = matriz[x1][y1];
            matriz[x1][y1] = matriz[x2][y2];
            matriz[x2][y2] = temp;  
            i++;
        }
        this.ArrayFigure.clear();
        
        for(int x = 0; x< SIZE; x++)
        {
            for(int y = 0; y<SIZE; y++)
            {
                super.add(matriz[x][y]);
                matriz[x][y].setBounds((SIZE)*100+50+(y+1)*100,(x)*100 +50, 100,100);
                this.ArrayFigure.add(matriz[x][y]);
            }
        }   
    }
    
    @Override
    public void mousePressed(MouseEvent e)
    {
        Figure fig;      
        if((fig=getPressed(e.getPoint())) != null)
        {
            fig.setPressed(true);
            this.posX = fig.getX();
            this.posY = fig.getY();
            if(gridIzquierdo.verZona(e.getPoint())){  // si se presiona uno que esta ya en la matriz
                gridIzquierdo.figuras[(((e.getY()+50)/100)-1)][(((e.getX()+50)/100)-1)] = null;   
            }
            System.out.println("fig copiada: " + this.posX + " " + this.posY);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {
        Figure fig = null;
        if((fig = getFigure())!= null)
        {
            boolean dentroDelPanel = this.gridIzquierdo.verZona(e.getPoint()); // fila y columna del grid
            if(dentroDelPanel) 
            {
                boolean movimientoValido = this.gridIzquierdo.setFigure(fig);
                if(!movimientoValido){
                    fig.setBounds(this.posX, this.posY, 100, 100);
                }      
            }
            else{  // cuando no está en el grid Izquierdo
               fig.setBounds(this.posX, this.posY, 100, 100);
            }
            repaint();
            fig.setPressed(false);
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        
    }
    
    @Override
    public void mouseDragged(MouseEvent e)
    {
        Figure fig;
        if((fig = getFigure())!= null)
        {
            fig.setBounds(e.getX()-50, e.getY()-50, 100, 100);
            this.repaint();
        }
    }
    
    private Figure getFigure()
    {
        for(Figure fig : this.ArrayFigure)
        {
            if(fig.isPressed() == true)
            {
                return fig;
            }
        }
        return null;
    }
    
    private Figure getPressed(Point p)
    {
        for(Figure fig: this.ArrayFigure)
        {
            if(fig.getX()<=p.getX() && fig.getWidth()+fig.getX()>=p.getX() && fig.getY()<=p.getY() && fig.getHeight()+fig.getY() >= p.getY())
            {
                return fig;
            }
        }
        return null;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        
    }
    
}
